
package com.netease.xone.fragment;

import android.app.ProgressDialog;
import android.content.Context;
import android.os.Bundle;
import android.view.inputmethod.InputMethodManager;
import android.widget.Toast;

import com.actionbarsherlock.app.SherlockFragment;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;
import com.actionbarsherlock.view.MenuItem;
import com.netease.log.NTLog;
import com.netease.pkgRelated.XoneConsts.MENU_ID;
import com.netease.xone.R;
import com.netease.xone.activity.ActivityDiscuss;
import com.netease.xone.activity.ActivityMain;
import com.netease.xone.dataMgr.ConfigChangeDataMgr;
import com.netease.xone.dataMgr.ParcelableArgument;
import com.netease.xone.widget.CustomDialog;
import com.netease.xone.widget.CustomDialog.OnCustomDlgCancelListener;

/***
 * 父的Fragment, 公共方法入口
 * 
 * @author echo
 * @since 2013-04-09
 */

public class FragmentBase extends SherlockFragment implements IConfigChangeInstance {

    private boolean mIsInited = true;
    private boolean mNeedLoad = true;
    private ProgressDialog mWaitingProgress;
    private CustomDialog mCustomDialog;

    /**
     * 保存数据
     */
    public Object onRetainCustomNonConfigurationInstance() {
        return null;
    }

    @Override
    public void onSave() {
        Object value = onRetainCustomNonConfigurationInstance();
        if (null != value) {
            ConfigChangeDataMgr.getInstance().put(getTag(), value);
        }
    }

    /**
     * 恢复数据
     * 
     * @param value
     */
    public void onRestoreCustomNonConfigurationInstance(Object value) {
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        Object value = ConfigChangeDataMgr.getInstance().get(getTag());
        if (null != value) {
            onRestoreCustomNonConfigurationInstance(value);
        }
        NTLog.i("FragmentBase", "onActivityCreated");
    }

    public void setInited(boolean isInited) {
        this.mIsInited = isInited;
    }

    public boolean isInited() {
        return mIsInited;
    }

    public void setNeedload(boolean needLoad) {
        this.mNeedLoad = needLoad;
        if (!needLoad) {
            mIsInited = false;
        }
    }

    public boolean isNeedLoad() {
        return mNeedLoad;
    }

    /**
     * 用于首次进入未加载数据的fragment,在切过去需要加载数据的情况
     */
    public void loadData() {
        if (!mIsInited) {
            if (onLoadData()) {
                mIsInited = true;
            }
        }
    }

    /**
     * 需子类重载的方法，已处理请返回 true
     */
    public boolean onLoadData() {
        return false;
    }

    /**
     * 用于中间刷新数据，子类需响应请重载
     */
    public void refreshData() {

    }
    /**
     * 设置是否显示菜单，默认显示，不显示的页面重载该方法返回false
     */
    public boolean needShowMenu() {
       return true;
    }

    /**
     * Toast 提示
     * 
     * @param resid
     */
    public void showToast(int resid) {
        showToast(resid, Toast.LENGTH_SHORT);
    }

    /**
     * Toast 提示
     * 
     * @param resid
     * @param duration
     */
    public void showToast(int resid, int duration) {
        Context context = getActivity();
        if(context != null) {
            Toast.makeText(context, resid, duration).show();
        }
    }

    /**
     * Toast 提示
     * 
     * @param message
     */
    public void showToast(String message) {
        Context context = getActivity();
        if(context != null) {
            Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
        }
    }

    /**
     * Toast 提示
     * 
     * @param message
     * @param duration
     */
    public void showToast(String message, int duration) {
        Context context = getActivity();
        if (context != null) {
            Toast.makeText(context, message, duration).show();
        }
    }

    /**
     * Toast 提示
     * 
     * @param message
     */
    public void showToast(CharSequence message) {
        Context context = getActivity();
        if (context != null) {
            Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
        }
    }

    /**
     * Toast 提示
     * 
     * @param message
     * @param duration
     */
    public void showToast(CharSequence message, int duration) {
        Context context = getActivity();
        if (context != null) {
            Toast.makeText(context, message, duration).show();
        }
    }
    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        if(getActivity() instanceof ActivityMain) {
            menu.add(Menu.NONE, MENU_ID.menu_to_discuss, 0, R.string.menu_discuss).setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER);
            menu.add(Menu.NONE, MENU_ID.menu_exit, 0, R.string.menu_exit).setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER);
        } else {
            if(needShowMenu()) {
            menu.add(Menu.NONE, MENU_ID.menu_to_discuss, 0, R.string.menu_discuss).setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER);
            menu.add(Menu.NONE, MENU_ID.menu_go_home, 0, R.string.menu_go_home).setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER);
            }
        }
        super.onCreateOptionsMenu(menu, inflater);
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                onLeftTopBack();
                return true;
            case MENU_ID.menu_to_discuss:
                ParcelableArgument arg = new ParcelableArgument(ParcelableArgument.TYPE_DEFAULT);
                ActivityDiscuss.launch(getActivity(), arg);
                return true;
            case MENU_ID.menu_go_home:
                InputMethodManager im = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
                if (im.isActive() && getActivity().getCurrentFocus() != null) {
                    im.hideSoftInputFromWindow(getActivity().getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
                }
                ActivityMain.launch(getActivity());
                return true;
            case MENU_ID.menu_exit:
                if(getActivity() instanceof ActivityMain){
                    ActivityMain main = (ActivityMain)getActivity();
                    main.onExit();
                }
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }
    protected void onLeftTopBack() {
//        getFragmentManager().popBackStack();
        getActivity().finish();
    }
    protected void showWatting(String message){
        showWatting(null, message);
    }
    protected void showWatting(String title, String message){
        if(mWaitingProgress != null)
            stopWaiting();
        
        mWaitingProgress = ProgressDialog.show(getActivity(), title, message, true, true);
    }
    protected void stopWaiting(){
        if(mWaitingProgress != null){
            mWaitingProgress.dismiss();
            mWaitingProgress = null;
        }
    }
    protected boolean isWaiting(){
        return (mWaitingProgress != null && mWaitingProgress.isShowing());
    }
    
    protected void showCustomDialog(Context context,String text,OnCustomDlgCancelListener listener){
        if(mCustomDialog != null){
            mCustomDialog.dismiss();
            mCustomDialog = null;
        }
        mCustomDialog = new CustomDialog(context,text,listener);
        mCustomDialog.show();
    }
    protected void closeCustomDialog(){
        if(mCustomDialog != null){
            mCustomDialog.dismiss();
            mCustomDialog = null;
        }
    }
    protected boolean isCustomDialogShow(){
        return (mCustomDialog != null && mCustomDialog.isShowing());
    }
    
}
